package com.lanxin.hr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanxin.hr.entity.*;
import com.lanxin.hr.service.*;
import com.lanxin.hr.vo.DepartmentJobVo;
import com.lanxin.hr.vo.EmpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cyh
 * @create 2025-07-16 11:03
 */
@Controller
@RequestMapping("/hr/emp")
public class ZzEmployeeController {

    @Autowired
    private IZzEmployeeService employeeService;

    @Autowired
    private IZzDepartmentService departmentService;

    @Autowired
    private IJcBasicdatavalueService basicdatavalueService;

    @Autowired
    private IZzJobinfoService jobinfoService;

    @Autowired
    private IZzAccountService accountService;

    @Autowired
    private IZzExprienceService exprienceService;

    @Autowired
    private IZzDepartmentJobService departmentJobService;

    @Autowired
    private IZzAdjustmentService adjustmentService;

    //根据部门id和职位id查询下属员工,用于添加部门经理
    @RequestMapping("/listEmpUseSelect")
    @ResponseBody
    public String listEmpUseSelect(@RequestParam(required = false) Integer deptid,@RequestParam(required = false) Integer jobid){
        List<ZzEmployee> employees=employeeService.listEmpUseSelect(deptid,jobid);
        ObjectMapper objectmapper = new ObjectMapper();
        String emps=null;
        try {
            emps=objectmapper.writeValueAsString(employees);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return emps;
    }

    //根据条件分页查询员工信息
    @RequestMapping("/listEmpVo")
    public ModelAndView listEmpVo(ZzEmployee employee,@RequestParam(required = false) Integer page,@RequestParam(required = false) Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        IPage<EmpVo> empVoIPage = employeeService.listEmpVo(employee,page,pageSize);

        //查询所有部门用于下拉框
        List<ZzDepartment> departments = departmentService.listDepartment();
        //查询所有职位用于下拉框
        List<JcBasicdatavalue> jobs = basicdatavalueService.getJobs();
        //查询所有的学历用于下拉框
        List<JcBasicdatavalue> degrees = basicdatavalueService.getDegrees();

        modelAndView.addObject("empVoIPage", empVoIPage);
        modelAndView.addObject("employee", employee);
        modelAndView.addObject("departments", departments);
        modelAndView.addObject("jobs", jobs);
        modelAndView.addObject("degrees", degrees);
        modelAndView.setViewName("hr/emp/emp_list");
        return modelAndView;
    }

    //前往添加员工页面
    @RequestMapping("/toAddEmp")
    public ModelAndView toAddEmp(ZzEmployee employee) {
        ModelAndView modelAndView = new ModelAndView();
        //获取全部部门用于下拉框
        List<ZzDepartment> departments = departmentService.listDepartment();
        //查询所有学历
        List<JcBasicdatavalue> degrees = basicdatavalueService.getDegrees();
        modelAndView.addObject("departments", departments);
        modelAndView.addObject("degrees", degrees);
        modelAndView.setViewName("hr/emp/addEmp");
        return modelAndView;
    }

    //添加员工及附加信息（账号，工作经验等）
    @RequestMapping("/addEmp")
    public String addEmp(ZzEmployee employee, ZzAccount account,ZzJobinfo jobinfo,@RequestParam(required = false) String[] startDate,
                         @RequestParam(required = false) String[] endDate, @RequestParam(required = false) String[] companyName,
                         @RequestParam(required = false) String[] deptName,@RequestParam(required = false) String[] jobName,
                         @RequestParam(required = false) String[] proofer,@RequestParam(required = false) String[] description,
                         @RequestParam(required = false) String[] expphone) {
        int size=0;
        if(proofer!=null&&proofer.length>0) {
            size=proofer.length;
        }
        List<ZzExprience> experiences = new ArrayList<ZzExprience>();
        if(size>0){
            for(int i=0;i<size;i++) {
                ZzExprience exprience= new ZzExprience();
                exprience.setStartDate(LocalDate.parse(startDate[i]));
                exprience.setEndDate(LocalDate.parse(endDate[i]));
                exprience.setCompanyName(companyName[i]);
                exprience.setDeptName(deptName[i]);
                exprience.setJobName(jobName[i]);
                exprience.setProofer(proofer[i]);
                exprience.setDescription(description[i]);
                exprience.setPhone(expphone[i]);
                experiences.add(exprience);
            }
        }
        employeeService.addEmp(employee,account,jobinfo,experiences);
        return "redirect:/hr/emp/listEmpVo";
    }

    //获取员工详情以及工作信息
    @RequestMapping("/empInfo")
    public ModelAndView empInfo(@RequestParam Integer oid) {
        ModelAndView modelAndView = new ModelAndView();
        //获取员工详情（empvo）
        EmpVo empVo = employeeService.getEmpVoByEid(oid);
        //获取员工工作信息
        ZzJobinfo jobinfo = jobinfoService.getJobinfoByEid(oid);
        modelAndView.addObject("empVo", empVo);
        modelAndView.addObject("jobinfo", jobinfo);
        modelAndView.setViewName("hr/emp/empInfo");
        return modelAndView;
    }

    //根据员工id回填员工信息，用于更新员工
    @RequestMapping("/toUpdateEmp")
    public ModelAndView toUpdateEmp(Integer oid) {
        ModelAndView modelAndView = new ModelAndView();
        //获取empVo
        EmpVo empVo = employeeService.getEmpVoByEid(oid);
        ZzAccount account = null;
        ZzJobinfo jobinfo = null;
        List<ZzExprience> experiences = new ArrayList<ZzExprience>();
        List<DepartmentJobVo> jobs = new ArrayList<DepartmentJobVo>();
        List<ZzDepartment> departments = new ArrayList<ZzDepartment>();
        if(empVo!=null) {
            //获取账号信息
            account = accountService.getAccountByEid(oid);
            //获取工作信息
            jobinfo = jobinfoService.getJobinfoByEid(oid);
            //获取工作经历
            experiences = exprienceService.getExprienceByEid(oid);
            //获取所有部门
            departments = departmentService.listDepartment();
            //查询员工所在部门下的职位
            if (empVo.getDeptid()!=null) {
                jobs  = departmentJobService.selectDepartmentJobByDId(empVo.getDeptid().toString());
            }
        }

        //查询所有学历
        List<JcBasicdatavalue> degrees = basicdatavalueService.getDegrees();
        modelAndView.addObject("empVo", empVo);
        modelAndView.addObject("account", account);
        modelAndView.addObject("jobInfo", jobinfo);
        modelAndView.addObject("experiences", experiences);
        modelAndView.addObject("departments", departments);
        modelAndView.addObject("jobs", jobs);
        modelAndView.addObject("degrees", degrees);
        modelAndView.setViewName("hr/emp/updateEmp");
        return modelAndView;
    }

    //更新
    @RequestMapping("/updateEmp")
    public String updateEmp(ZzEmployee emp, ZzAccount account, ZzJobinfo jobinfo,@RequestParam(required = false) String[] startDate,
                            @RequestParam(required = false) String[] endDate, @RequestParam(required = false) String[] companyName,
                            @RequestParam(required = false) String[] deptName,@RequestParam(required = false) String[] jobName,
                            @RequestParam(required = false) String[] proofer,@RequestParam(required = false) String[] description,
                            @RequestParam(required = false) String[] expphone) {
        int size=0;
        if(proofer!=null&&proofer.length>0) {
            size=proofer.length;
        }
        List<ZzExprience> experiences = new ArrayList<ZzExprience>();
        if(size>0){
            for(int i=0;i<size;i++) {
                ZzExprience exprience= new ZzExprience();
                exprience.setStartDate(LocalDate.parse(startDate[i]));
                exprience.setEndDate(LocalDate.parse(endDate[i]));
                exprience.setCompanyName(companyName[i]);
                exprience.setDeptName(deptName[i]);
                exprience.setJobName(jobName[i]);
                exprience.setProofer(proofer[i]);
                exprience.setDescription(description[i]);
                exprience.setPhone(expphone[i]);
                experiences.add(exprience);
            }
        }
        //更新员工信息
        if(experiences.size()==0) {
            experiences=null;
        }
        employeeService.updateEmp(emp,account,jobinfo,experiences);
        return "redirect:/hr/emp/listEmpVo";
    }

    //更新员工状态
    @ResponseBody
    @RequestMapping("/updateEmpStatue")
    public String updateEmpStatue(@RequestParam Integer oid, @RequestParam String state) {
        boolean s=employeeService.updateEmpStatus(oid,state);
        ObjectMapper mapper = new ObjectMapper();
        String json=null;
        try {
            json=mapper.writeValueAsString(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

    //删除员工
    @RequestMapping("/deleteEmp")
    @ResponseBody
    public String deleteEmp(@RequestParam Integer oid) {
        boolean s = employeeService.deleteEmp(oid);
        ObjectMapper mapper = new ObjectMapper();
        String json=null;
        try {
            json = mapper.writeValueAsString(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }


    //获取员工vo
    @RequestMapping("/getEmpVoById")
    @ResponseBody
    public String getEmpVoById(@RequestParam Integer oid) {
        EmpVo empVo = employeeService.getEmpVoByEid(oid);
        ObjectMapper mapper = new ObjectMapper();
        String json=null;
        try {
            json = mapper.writeValueAsString(empVo);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }


}
